﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using MyQuestMobApp.StudentService;
using System.IO;
using MyQuestMobApp.pages;


namespace MyQuestMobApp.pages
{
    public partial class student_avatar : PhoneApplicationPage
    {
        private int i;     // index of the imageList
        private int l; // length of the imageList
        static Random r = new Random();  // random number
        private int tmp;        // temporary variable
        private List<BitmapImage> imageList;
        BitmapImage imageSource = null;
        public int student_id;
        public student_avatar(int student_id)
        {
            InitializeComponent();
            this.student_id = student_id;
            imageList = new List<BitmapImage>();
            buildTheList();
        }
        private void buildTheList()
        {
            var studentSrvClient = new StudentSrvClient();

            studentSrvClient.getStudentAvatarCompleted += new EventHandler<getStudentAvatarCompletedEventArgs>(serviceClient_getStudentAvatarCompleted);
            studentSrvClient.getStudentAvatarAsync(this.student_id); 

            studentSrvClient.getAllAvatarsCompleted += new EventHandler<getAllAvatarsCompletedEventArgs>(serviceClient_getAllAvatarsCompleted);
            studentSrvClient.getAllAvatarsAsync();
        }
        private void nextPicture(object sender, System.Windows.RoutedEventArgs e)
        {
            i++;
            if (i > l - 1) 	// wrap around if reach the lighest number
            {
                i = i - l;
            }
            avatar_imgBox.Source = imageList[i]; // update source of img
        }

        private void previousPicture(object sender, System.Windows.RoutedEventArgs e)
        {

            i--;
            if (i < 0)					// wrap around if reach the lowest number
            {
                i = l - 1;
            }
            avatar_imgBox.Source = imageList[i];		 // update source of img
        }

        private void serviceClient_updateAvatarCompleted(object updateAvatarByIDCompletedEventArgs, updateAvatarByIDCompletedEventArgs e)
        {
            MessageBox.Show("inside_event");
        }

        private void serviceClient_getAvatarByIDCompleted(object getAvatarByIDCompletedEventArgs, getAvatarByIDCompletedEventArgs e)
        {

            using (MemoryStream stream = new MemoryStream(e.Result.picture))
            {
                stream.Seek(0, SeekOrigin.Begin);
                BitmapImage b = new BitmapImage();
                b.SetSource(stream);
                imageSource = b;
            }

            //img1.Source = imageSource;
            imageList.Add(imageSource);
        }

        private void serviceClient_getAllAvatarsCompleted(object getAllAvatarsCompletedEventArgs, getAllAvatarsCompletedEventArgs e)
        {
            List<BitmapImage> bitmapImageList = new List<BitmapImage>();
            foreach (Avatar iterator in e.Result.ToList())
            {
                using (MemoryStream stream = new MemoryStream(iterator.picture))
                {
                    BitmapImage b = new BitmapImage();
                    stream.Seek(0, SeekOrigin.Begin);
                    b.SetSource(stream);
                    imageList.Add(b);
                    //avatar_imgBox.Source = imageList[0];
                }
            }
            l = imageList.Count;
        }

        private void serviceClient_getStudentAvatarCompleted(object sender, getStudentAvatarCompletedEventArgs e)
        {
            avatar_imgBox.Source = MyQuestApp.utils.ImageConvertor.BinaryToBitmapImage(e.Result);
        }

        private void generateRandomAvatar(object sender, System.Windows.RoutedEventArgs e)
        {
            tmp = i;
            i = r.Next(l);
            while (i == tmp)
            {
                i = r.Next(l);
            }
            avatar_imgBox.Source = imageList[i];		 // update source of img
        }
        private void accept(object sender, System.Windows.RoutedEventArgs e)
        {
            var studentSrvClient = new StudentSrvClient();
            studentSrvClient.getStudentAvatarCompleted += new EventHandler<getStudentAvatarCompletedEventArgs>(serviceClient_getStudentAvatarCompleted);
            studentSrvClient.getStudentAvatarAsync(this.student_id); 
            //NavigationService.Navigate(new Uri("/login.xaml", UriKind.Relative));
            
        }

        private void search_avatar_xbox_Click(object sender, RoutedEventArgs e)
        {
            avatar_imgBox.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
            Uri uri = new Uri("http://avatar.xboxlive.com/avatar/" + xbox_textBox.Text + "/avatar-body.png", UriKind.Absolute);
            avatar_imgBox.Source = new BitmapImage(uri);           
        }

        void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            // Show the default image
        }

        private void back_Click(object sender, RoutedEventArgs e)
        {
            this.Content = new student_mainPage(this.student_id,"user_name");
        }
    }
}